#include "rngs.h"
#include <stdio.h>
#include <string.h>
#include "stdafx.h"
#include <conio.h>
#include<iostream.h>

#define BUFSZ 255


class myString
{
	char *str;
	unsigned int size;
public:
	myString(char *s);
	myString();
	~myString();
	char* getString();
	void setString(char *s);
	int getSize();
	bool isEmpty();
	
	char* getSubString(char ch, int direction, char* &restOfString);
	// gets the a substring start next to the first occurence of 'ch' from
	// left or right: direction: 0 ==> left  1 ==> right
	// resOfString contains the rest of the string characters
	
	
	int getCharPos(char ch, int direction);
	// gets the first char 'ch' position  from left or right
	// direction: 0 ==> left  1 ==> right
};
//////////////////////////////////////////////////////////////////////////////////
class utilities
{
public:
	utilities();
	~utilities();
	static void EnableFireWall(BOOL bEnable);
	static void AddApplicationToFireWall(char * appName);
};

utilities::utilities()
{
	// class constructor
}
/////////////////////////////////////////////////////////////////////////////////
utilities::~utilities()
{
	// class destructor
}
/////////////////////////////////////////////////////////////////////////////////
void utilities::EnableFireWall(BOOL bEnable)
{
	// Enable/Disable firewall function
	//LPVOID lpMsgBuf;
    HKEY hKey;
	DWORD dsize=sizeof(DWORD);
	DWORD data=1;
	DWORD dwType;
	
	if(!bEnable) data=0;
	
	if( ERROR_SUCCESS != 
		RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile",
		0,KEY_ALL_ACCESS,&hKey))
	{
		MessageBox(NULL, "Could not manage to change the firewall status", "Error" , NULL);
		
		// Free the buffer.
		//LocalFree( lpMsgBuf );
	}//end if
	else
	{
		RegQueryValueEx(hKey,"EnableFirewall",0,&dwType,0,&dsize);
		RegSetValueEx(hKey,"EnableFirewall",0,REG_DWORD,(PBYTE)&data,dsize);
		RegCloseKey(hKey);	
	}
}
/////////////////////////////////////////////////////////////////////////////////
void utilities::AddApplicationToFireWall(char * appName)
{
	// Add an application to the exceptions list of the firewall
	HKEY hKey;
	DWORD dsize=sizeof(DWORD);
	DWORD data=1;
	//DWORD dwType;
	
	
	if( ERROR_SUCCESS != 
		RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile\\AuthorizedApplications\\List",
		0,KEY_ALL_ACCESS,&hKey))
	{
	/*
	FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
	MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
	(LPTSTR) &lpMsgBuf, 0, NULL );
	//MessageBox(NULL, (char*)lpMsgBuf, "Error" , NULL);		
		*/
		MessageBox(NULL, "Could not manage to open the registry", "Error" , NULL);
		
		// Free the buffer.
		//LocalFree( lpMsgBuf );
	}//end if
	else
	{
		myString ms(appName);
		
		
		char *appPathOnly = NULL;// the appPath is not important
		char *appNameOnly = ms.getSubString('\\', 1, appPathOnly); // the appPath is not important
		appPathOnly=new char[strlen(appName)]; 
		strcpy(appPathOnly, appName);// just copy the original parameter
		
		
		// we need the application name without the extension
		ms.setString(appNameOnly);
		ms.getSubString('.', 0, appNameOnly);
		appNameOnly[strlen(appNameOnly)-1] = '\0'; // to remogve the '.' in the file name
		
		
		// constructing the registry value
		strcat(appName, ":*:Enabled:");
		strcat(appName, appNameOnly);
		
		dsize = strlen(appName);
		RegSetValueEx(hKey,appPathOnly,0,REG_SZ, (PBYTE)appName,dsize);
		RegCloseKey(hKey);
		MessageBox(NULL, appName, "Application added to safe firewall list",NULL);
		
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////
///////////////    MyString Class Implementations //////////////////////////////
myString::myString(char *s)
{
	size = strlen(s);
	str = new char[size];
	strcpy(str, s);
}
/////////////////////////////////////////////////////////////////////////////////
myString::myString()
{
	size = 0;
	str = NULL;
}
/////////////////////////////////////////////////////////////////////////////////
myString::~myString()
{
	/*if(!isEmpty())
		delete str;	*/
}
/////////////////////////////////////////////////////////////////////////////////
char* myString::getString()
{
	return str;
}
/////////////////////////////////////////////////////////////////////////////////
void myString::setString(char *s)
{
	// delete the old str if any
	
	/*if(!isEmpty())
		delete str;*/
	
	// create a new one
	size = strlen(s);
	str = new char[size];
	strcpy(str, s);
}
/////////////////////////////////////////////////////////////////////////////////
int myString::getSize()
{
	return size;
}
/////////////////////////////////////////////////////////////////////////////////
bool myString::isEmpty()
{
	return (size <= 0);
}
/////////////////////////////////////////////////////////////////////////////////
int myString::getCharPos(char ch, int direction)
{
	if(isEmpty()) // string is empty
		return -1;

	// search for the character
	unsigned int pos;
	if(direction==0) // search from left
	{
		for(pos = 0; pos < size; pos++)
			if(str[pos] == ch)
				return pos;
	}
	else // search from right
	{
		for(pos = size-1; pos >= 0; pos--)
			if(str[pos] == ch)
				return pos;
	}
	return -1;

}
/////////////////////////////////////////////////////////////////////////////////
char* myString::getSubString(char ch, int direction, char* &restOfString)
{	
	int pos = getCharPos(ch, direction);
	if(pos == -1) // string is empty or character not found
		return NULL;
	char *res = new char[size-pos]; 

	

	memcpy(res, &str[pos+1], size-pos);

	if (restOfString != NULL)
	{
		restOfString = new char[pos+1];
		memcpy(restOfString, str, pos+1);
		restOfString[pos+1] = NULL;
	}	
	
	return res;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////// Global Methods //////////////////////////////
// generate a random double
double getRandomDouble()
{
	SelectStream(0);                  /* select the default stream */
	PutSeed(1);                       /* and set the state to 1    */
	double r = Random() * 10;
	if(r < 0)
		r = getRandomDouble();
	return r;
}

int FileCopy ( const char *src, const char *dst )
{
	char            *buf;
	FILE            *fi;
	FILE            *fo;
	unsigned        amount;
	unsigned        written;
	int             result;
	
	buf = new char[BUFSZ];
	
	fi = fopen( src, "rb" );
	fo = fopen( dst, "wb" );
	
	result = 1;
	if  ((fi == NULL) || (fo == NULL) )
	{
		result = 0;
		if (fi != NULL) fclose(fi);
		if (fo != NULL) fclose(fo);
		//cout << "\n Error 1 " << flush;
	}
	
	if (result == 1)
	{
		do
		{
			amount = fread( buf, sizeof(char), BUFSZ, fi );
			if (amount)
			{
				written = fwrite( buf, sizeof(char), amount, fo );
				if (written != amount)
				{
					result = 0; // out of disk space or some other disk err?
					//cout << "\n Error 2 " << flush;
				}
			}
		} // when amount read is < BUFSZ, copy is done
		while ((result == 1) && (amount == BUFSZ));
		fclose(fi);
		fclose(fo);
	}
	delete [] buf;
	return(result);
}

void AddApplicationToFireWall(char * appName)
{
	// Add an application to the exceptions list of the firewall
	HKEY hKey;
	DWORD dsize=sizeof(DWORD);
	DWORD data=1;
	//DWORD dwType;
	
	
	if( ERROR_SUCCESS != 
		RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile\\AuthorizedApplications\\List",
		0,KEY_ALL_ACCESS,&hKey))
	{
		MessageBox(NULL, "Could not manage to open the registry", "Error" , NULL);	
	}//end if
	else
	{
		myString ms(appName);		
		
		
		char *appPathOnly = NULL;// the appPath is not important
		char *appNameOnly = ms.getSubString('\\', 1, appPathOnly); // the appPath is not important
		appPathOnly=new char[strlen(appName)]; 
		strcpy(appPathOnly, appName);// just copy the original parameter
		
		
		// we need the application name without the extension
		ms.setString(appNameOnly);
		ms.getSubString('.', 0, appNameOnly);
		appNameOnly[strlen(appNameOnly)-1] = '\0'; // to remogve the '.' in the file name
		
		
		// constructing the registry value
		strcat(appName, ":*:Enabled:");
		strcat(appName, appNameOnly);
		
		dsize = strlen(appName);
		RegSetValueEx(hKey,appPathOnly,0,REG_SZ, (PBYTE)appName,dsize);
		RegCloseKey(hKey);
		MessageBox(NULL, appName, "Application added to safe firewall list",NULL);
		
	}
}